
//Copyright 1997-2009 Syrinx Development, Inc.
//This file is part of the Syrinx Web Application Framework (SWAF).
// == BEGIN LICENSE ==
//
// Licensed under the terms of any of the following licenses at your
// choice:
//
//  - GNU General Public License Version 3 or later (the "GPL")
//    http://www.gnu.org/licenses/gpl.html
//
//  - GNU Lesser General Public License Version 3 or later (the "LGPL")
//    http://www.gnu.org/licenses/lgpl.html
//
//  - Mozilla Public License Version 1.1 or later (the "MPL")
//    http://www.mozilla.org/MPL/MPL-1.1.html
//
// == END LICENSE ==
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;
using System.Xml;

using Swaf;
using Swaf.BizObj;
using Swaf.BizRule;
using Swaf.Container;
using Swaf.Security;

namespace eCom
{
	public class CommunityManagementBizRules : Syrinx.BaseBizRules
	{
		/// <summary>
		/// This is a very specific bizrule that will look to see if it should add community pricing options
		/// into the list returned from the getProducts bizrule.  0
		/// </summary>
		/// <param name="app"></param>
		/// <param name="context"></param>
		/// <param name="returnValue"></param>
		/// <param name="inputParams"></param>
		/// <returns></returns>
		public IBizObjList communityGetProductsPostExtend(IApplication app, IRuleContext context, object returnValue, object[] inputParams)
		{
			IBizObjList products = returnValue as IBizObjList;
			if (products != null && products.numElements == 0)
			{
				INameAddressSupport info = inputParams[2] as INameAddressSupport;
				object parentId = null;
				if (info != null && (parentId = info.get("parentId")) != null && parentId is Swaf.BGuid.Guid)
				{
					IResults rc = context.RuleGroup.execute(context, "getProductDetails", parentId, false, DetailLevel.idOnly, false, true);
					IBizObj prod = rc.CallResults as IBizObj;
					if (prod != null && ((string)prod.get("partId")).StartsWith("CCM-", true, null))
					{
						INameAddressSupport m = new FlexiMap();
						m.put("parentId", "CommunitySitePackageLevels");
						m.put("companyId", info.get("companyId"));
						m.put("includeSelf", info.get("includeSelf"));
						m.put("listDetailLevel", info.get("listDetailLevel"));
						m.put("includeTranslations", info.get("includeTranslations"));

						rc = context.RuleGroup.execute(context, "getProducts", m, (IBizObj)null);
						IBizObjList packageLevels = rc.CallResults as IBizObjList;
						if (packageLevels != null)
						{
							//Normally it is best to get this id from a rule call, if that code is outside of 
							//the eCom package.
							Swaf.BGuid.Guid currentId = app.sessionData["CurrentOrderId"] as Swaf.BGuid.Guid;
							IKeyedBizObjList orderItems = null;
							IBizObj orderItem = null;
							if (currentId != null)
							{
								rc = context.RuleGroup.execute(context, "getSalesOrderItems", currentId);
								if (rc.CallResults is IBizObjList)
									orderItems = ((IBizObjList)rc.CallResults).getIndex("calculatedProductPartId");
							}
							foreach (IBizObj package in packageLevels)
							{
								string combinedPartIds = new StringBuilder(50).Append(prod.get("partId")).Append(package.get("partId")).ToString();
								string combinedIds = new StringBuilder(50).Append(package.get("id")).Append('|').Append(prod.get("id")).ToString();
								if (orderItems != null && (orderItem = orderItems.find(combinedIds, false)) != null)
									package.put("id", orderItem.get("product.id"));
								//else
								//	package.put("id", new Swaf.BGuid.Guid());
								package.put("combinedId", combinedIds);
								package.put("partId", combinedPartIds);
								package.put("isTempId", true);
							}

							products = packageLevels;
						}
					}
				}
			}

			return products;
		}
	}
}
